<?php
class Back_GalerieAlbumsController extends Osml_Controller_Admin_Action
{

    public function indexAction()
    {
        $model = new Back_Model_GalerieAlbums();
        $select = new Zend_Db_Table_Select($model->_getMapper()->_getTable());
        
        $form = null;
        
        $this->view->tableIdField = 'id_albums';
        $this->view->tableSelect = $select;
        $this->view->tableHeader = array(
            'titre' => array(
                'name' => __("Titre"), 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'proprietaire' => array(
                'name' => __("Propriétaire"), 
                'link' => array(
                    'action' => 'editer'
                )
            )
        );
        
        /*
         * Thierry BROUARD (BIIS) callback = array( $objet, $method ) link =
         * array( action [,controller[,module]] ) ou array( 'callback' => array(
         * $objet, $method ) )
         */
        $this->view->tableOptions = array(
            'callback' => array(
                $this, 
                '_transformRowsCallback'
            ),  // Transformation callback
            'link' => array(
                'action' => 'voir'
            ), 
            'pageNumber' => (int) $this->_getParam('page', 1), 
            'pageItemPerPage' => (int) $this->_getParam('par', 10), 
            'form' => $form
        );
    }

    public function editerAction()
    {
        $requete = $this->getRequest();
        $id = $requete->getParam("id", 0);
        
        $utilisateurs = new Back_Model_Utilisateurs();
        $utilisateursSelect = $utilisateurs->listerPourSelect(true, "pseudo");
        $options['utilisateurs'] = $utilisateursSelect;
        
        $form = new Back_Form_GalerieAlbums('Formulairejndpro', $options);
        $model = new Back_Model_GalerieAlbums($id);
        
        if ($requete->isPost()) {
            if ($form->isValid($requete->getPost())) {
                $date = date(Jnd_Global_Constantes::FORMAT_DATE_BASE);
                if (intval($id) > 0) {
                    $model->charger($id);
                    $model->setOptions($form->getValues());
                } else {
                    $model->setOptions($form->getValues());
                }
                $resultat = $model->sauver();
                $id = $model->getId();
                
                $rep = APPLICATION_PATH . DIRECTORY_SEPARATOR . "galerie" . DIRECTORY_SEPARATOR . preg_replace("/\s/", "", strtolower($model->getTitre()));
                if (! file_exists($rep)) {
                    mkdir($rep);
                }
                
                return $this->_helper->redirector('index');
            }
        } elseif ($requete->isGet()) {
            if ($id > 0) {
                $model->charger($id);
                $form->populate($model->toArray());
            }
        }
        $this->view->form = $form;
        $this->view->model = $model;
        
        $upload = new Back_Form_Upload("Formulaire");
        $this->view->upload = $upload;
    }

    public function supprimerAction()
    {
        $request = $this->getRequest();
        $id = $request->getParam("id", 0);
        
        if ($id > 0) {
            $model = new Back_Model_GalerieAlbums($id);
        }
    }

    /**
     *
     * @see Jnd_Controller_Action::_transformRowsCallback()
     */
    public function _transformRowsCallback( $row, $view )
    {
        if (isset($row["proprietaire"])) {
            $utilisateurs = $row['proprietaire'];
            if (strpos(substr($utilisateurs, 1), ";") === false) {
                $utilisateurs = (array) $utilisateurs;
            } else {
                $utilisateurs = substr($utilisateurs, 1, strlen($utilisateurs) - 2);
                $utilisateurs = preg_split("#;#", $utilisateurs);
            }
            $tmp = "";
            foreach ($utilisateurs as $id) {
                $user = new Back_Model_Utilisateurs($id);
                $tmp .= ", " . $user->getPseudo();
            }
            $row['proprietaire'] = substr($tmp, 2);
        }
    }

    public function analyseAction()
    {
        $this->desactivationRendu();
        $acl = new Osml_Acl();
    }
}

